<?php
namespace App\Models\PartnerModels;
use App\Models\ParentModel;
use DB;
/**
 * 移动端model
 * @author puppet
 */
class Mobile extends ParentModel{
  /**
   * 与模型关联的数据表。
   *
   * @var string
   */
  protected $table = 'pt_mobile';

  /**
   * DB::直接操作的数据库表名
   * @var string
   */
  protected $db_table = 'pt_mobile';

  /**
   * 主键 默认id。
   *
   * @var string
   */
  protected $primaryKey = 'id';

  /**
   * 指定是否模型应该被戳记时间。
   *
   * @var bool
   */
  public $timestamps = false;
  
  /**
   * type类型 0.全部 1.合伙人 2.会员
   */
  public function getAllData($type = 0){
    $db = DB::table($this->table)->select('id','user_name','name','mobile')->where('status',1);
    if(intval($type) > 0){
      $db = $db->where('type',$type); 
    }
    return $db->get();
  }
    // 该账户名是否可用
    public function checkUname($uname){
        $resout=array();
        if(!empty($uname)){
            $sql="select user_name from $this->table where user_name=? and status!=-1";
            // echo $sql;exit();
            $resout=DB::select($sql,[$uname]);
        }
        return !empty($resout) ? $resout[0] : false;
    }
  
	/**
	* 翻页展示所有信息    移动端列表
	*/
	public function getMobilepPageData($params){
		$res = false;
		$size = (isset($params['limit']) && intval($params['limit'])>0 && intval($params['limit']) <= 100) ? $params['limit'] : 10;
		$page = isset($params['page']) ? intval($params['page']) : 1;
		$offset = ($page - 1) * $size;
        $countSql="SELECT count(*) as count FROM {$this->db_table} WHERE `status` >= 1";
        $sql="select id,fid,type,user_name,nickname,gender,mobile,start_date,end_date,create_time,update_time,status from $this->table where status >=0 ";

		if(isset($params['user_name']) && !empty($params['user_name'])){
			$countSql .= " AND user_name like '%{$params['user_name']}%'";
			$sql .= " AND user_name like '%{$params['user_name']}%'";
		}
		if(isset($params['nickname']) && !empty($params['nickname'])){
			$countSql .= " AND nickname like '%{$params['nickname']}%'";
			$sql .= " AND nickname like '%{$params['nickname']}%'";
		}
		if(isset($params['mobile']) && !empty($params['mobile'])){
			$countSql .= " AND mobile like '%{$params['mobile']}%'";
			$sql .= " AND mobile like '%{$params['mobile']}%'";
		}
		if(isset($params['order']) && !empty($params['order'])){
			$order_db = ['id'];
			$sql .= " ORDER BY ";
			foreach ($params['order'] as $order){
				$result = isset($order_db[$order['column']]) ? $order_db[$order['column']] : '';
				!empty($result) && $sql .=  $result.' '.$order['dir'].',';
			}
			$sql = substr($sql,0,-1);
		}
		$count = DB::select($countSql);
		$count = isset($count[0]['count']) ? $count[0]['count'] : 0;
		$sql .= " LIMIT $offset,$size";
		$data = DB::select($sql);
		if(!empty($data)){
			$i=0;
			foreach($data as $k=>$v){
				++$i;
				$data[$k]['kid']=$i;
			//	$userImg=!empty($v['userImg']) ? $v['userImg'] : '/images/default.png';
			//	$data[$k]['userImg']="<img src=\"{$userImg}\" width=\"60\" height=\"60\">";
				$data[$k]['gender']=$v['gender']==1 ? '男' : ($v['gender']==2 ? '女' : '未知');
				$data[$k]['date']=$v['start_date']."——".$v['end_date'];
				$data[$k]['status']='';
				if($v['status']==0){
					$data[$k]['status']="<div class=\"checkbox checkbox-slider--b-flat\"><label><input type=\"default\" checked onclick=\"actionList({$v['id']},1)\"><span></span></label></div>";
				}elseif($v['status']==1){
					$data[$k]['status']="<div class=\"checkbox checkbox-slider--b-flat\"><label><input type=\"checkbox\" checked onclick=\"actionList({$v['id']},0)\"><span></span></label></div>";
				}
				$data[$k]['operation']="<a href='javascript:;' title='查看' aria-label='View' data-pjax='0'><span class='glyphicon glyphicon-eye-open' onclick=\"viewVip({$v['id']},'{$v['user_name']}')\"></span></a>";
				$data[$k]['operation'].="&nbsp;&nbsp;&nbsp;&nbsp;";
				$data[$k]['operation'].="<a href='/admin/mobile/edit/{$v['id']}' title='修改' aria-label='Update' data-pjax='0'><span class='glyphicon glyphicon-pencil'></span></a>";
				$data[$k]['operation'].="&nbsp;&nbsp;&nbsp;&nbsp;";
				$data[$k]['operation'].="<a href='javascript:;' title='删除' aria-label='Delete' data-pjax='0' onclick='actionList({$v['id']},-1)'><span class='glyphicon glyphicon-trash'></span></a>";
		        }
		}
		// echo "<pre/>";print_r($data);exit;
		$res['data'] = $data;
		$res['total'] = $count;
		//总条数记录
		return $res;
	}
  

	
	//查看合伙人或者会员详情 0 合伙人   1 会员
	public function listOne($data,$type=0){
		$i=0;$values=array();$resout=array();
		if(is_array($data)){
			$sql="select a.*,b.pay_type,b.ca_number,b.remark,d.name as idsname from $this->table as a";
			$sql.=" left join pt_order as b on b.partner_id=a.id";
			if($type){
				$sql.=" left join pt_user as c on c.partner_id=a.id";
			}
			$sql.=" left join pt_industry as d on d.id=a.industry_id";
			$sql.=" where";
			foreach($data as $k=>$v){
				$values[]=$v;
				if($i==0){
					$sql.=" $k=?";
				}else{
					$sql.=" and $k=?";
				}
				$i++;
			}
			$sql.=" limit 1";
			// echo $sql;exit();
			// var_dump($values);exit();
			$list=DB::select($sql,$values);
			if(!empty($list)){
				$resout=$list[0];
			}
		}
		return $resout;
	}
	
	//拉取会员套餐详情
	function selectMeal($partner_id){
		$path='/config/inquiry.json';
		$inquiry=\App\Components\Tools::getThird($path);
		$data=DB::select("select * from pt_partner_meal where partner_id = ? and list_type = 1",[$partner_id]);
		$html='';
		if(isset($data[0])){
			//已使用视频上传数
			$num1=DB::select("select count(id) as count from pt_dy_user_video where partner_id=?",[$partner_id]);
			$num1=isset($num1[0]['count']) ? $num1[0]['count'] : 0;
			
			//已使用询盘次数
			$num2=DB::select("select count(id) as count from pt_partner_comment where partner_id=?",[$partner_id]);
			$num2=isset($num2[0]['count']) ? $num2[0]['count'] : 0;
			$vNum=DB::select("select count(id) as count from pt_user_info_comment where partner_id=?",[$partner_id]);
			$vNum=isset($vNum[0]['count']) ? $vNum[0]['count'] : 0;
			$num2=$num2+$vNum;
			
			//已使用视频雷达数
			$num3=DB::select("select count(id) as count from pt_user_info where info_type=2 and system_type=0 and partner_id=?",[$partner_id]);
			$num3=isset($num3[0]['count']) ? $num3[0]['count'] : 0;
			
			//已使用账户雷达数
			$num4=DB::select("select count(id) as count from pt_user_info where system_type=0 and partner_id=? and info_type=1",[$partner_id]);
			$num4=isset($num4[0]['count']) ? $num4[0]['count'] : 0;
			
			foreach($data as $k=>$v){
				$html.="<tr>";
					if($v['list_type']==1){
						$listType='询盘';
						$text="视频上传数：{$v['num1']} 个<br/>询盘次数：{$v['num2']} 次<br/>视频雷达数：{$v['num3']} 个<br/>账户雷达数：{$v['num4']} 个";
					}
					$html.="<td>{$inquiry[$v['meal_name']]['iqname']}</td>";
					$html.="<td>{$v['start']} <br/> {$v['end']}</td>";
					$html.="<td>{$v['price']} 元</td>";
					$html.="<td>{$text}</td>";
				$html.="</tr>";
				$html.="<tr style=\"color:red;\"><td>剩余套餐数</td><td colspan=\"4\" >";
					$html.="视频上传数：".(($v['num1']-$num1)>0 ? ($v['num1']-$num1) : 0)." 个<br/>";
					$html.="询盘次数：".(($v['num2']-$num2)>0 ? ($v['num2']-$num2) : 0)." 次<br/>";
					$html.="视频雷达数：".(($v['num3']-$num3)>0 ? ($v['num3']-$num3) : 0)." 个<br/>";
					$html.="账户雷达数：".(($v['num4']-$num4)>0 ? ($v['num4']-$num4) : 0)." 个";
				$html.="</td></tr>";
			}
		}else{
			$html="<tr><td colspan=\"6\">无套餐记录</td></tr>";
		}
		return $html;
	}
	
	//拉取合伙人详情
	function viewInfo($id){
		$sql="select a.*,b.pay_type,b.ca_number from pt_partner as a left join pt_order as b on b.partner_id=a.id";
		$sql.=" where a.id=? and b.partner_type=1 and b.type=3";
		$re=DB::select($sql,[$id]);
		$re=isset($re[0]['id']) ? $re[0] : false;
		$html="";
		if($re){
			$html.="<p>地区：{$re['city']}</p>";
			$html.="<p>合伙机构：{$re['company']}</p>";
			$html.="<p>联系人：{$re['name']}</p>";
			$html.="<p>手机号：{$re['mobile']}</p>";
			$gender=$re['gender']==0 ? "未知" : ($re['gender']==1 ? "男" : "女");
			$html.="<p>性别：{$gender}</p>";
			$html.="<p>地址：{$re['address']}</p>";
			$html.="<p>微信：{$re['wechat']}</p>";
			$html.="<p>邮箱：{$re['email']}</p>";
			$html.="<p>开户比例：{$re['discount']} </p>";
			$html.="<p>开户金额：{$re['first_price']} 元</p>";
			$html.="<p>余额：{$re['balance']} 元</p>";
			$html.="<p>创建时间：{$re['create_time']}</p>";
			$html.="<p>修改时间：{$re['update_time']}</p>";
			if($re['pay_type']==4){
				$html.="<p>对公账号：{$re['ca_number']}</p>";
			}else{
				$config=Config('basics.pay_type');
				$html.="<p>支付方式：{$config[$re['pay_type']]}</p>";
			}
			
		}
		return $html;
	}
	
	//拉取会员详情
	function viewVipInfo($id){// 这是是默认admin创建 所以是左查询的ptadmin表
		$sql="select a.*,b.admin_name from pt_mobile as a left join pt_admin as b on a.fid =b.admin_id where a.id=? ";
		$re=DB::select($sql,[$id]);
		$re=isset($re[0]['id']) ? $re[0] : false;
		//print_r($re);exit();
		$html="";
		if($re){
			$html.="<p>昵称：{$re['nickname']}</p>";
			$html.="<p>手机号：{$re['mobile']}</p>";
			$gender=$re['gender']==0 ? "未知" : ($re['gender']==1 ? "男" : "女");
			$html.="<p>性别：{$gender}</p>";
            $gtype=$re['gender']==0 ? "未知" : ($re['gender']==1 ? "xxapp" : "小程序");
			$html.="<p>类型：{$gtype}</p>";
			$html.="<p>账号创建者：{$re['admin_name']}</p>";
			$html.="<p>创建时间：{$re['create_time']}</p>";
			$html.="<p>修改时间：{$re['update_time']}</p>";
		}
		return $html;
	}

	//会员套餐 添加、修改
	function saveTaocan($data,$partner_id){
		if($partner_id){
			$sql="update pt_partner_meal set ";
			foreach($data as $k=>$v){
				$sql.="`".$k."`='".$v."',";
			}
			$sql=substr($sql,0,-1)." where partner_id={$partner_id}";
			DB::update($sql);
		}else{
			DB::table("pt_partner_meal")->insertGetId($data);
		}
	}
	


	
	//查询指定会员信息
	function selectVip($partner_id){
		$sql="select * from pt_mobile where status>=0 and id=?";
		$re=DB::select($sql,[$partner_id]);
		return isset($re[0]['id']) ? $re[0] : false;
	}


}